package com.zzz.robot201.mvp.ui.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.zzz.robot201.R;
import com.zzz.robot201.app.base.BaseViewActivity;
import com.zzz.robot201.app.config.AppContent;
import com.zzz.robot201.di.component.DaggerEquipmentComponent;
import com.zzz.robot201.mvp.contract.EquipmentContract;
import com.zzz.robot201.mvp.model.api.bean.BaseBean;
import com.zzz.robot201.mvp.model.api.bean.EquipmentBean;
import com.zzz.robot201.mvp.presenter.EquipmentPresenter;
import com.zzz.robot201.mvp.ui.adapter.EquipmentAdapter;
import com.zzz.robot201.utils.SpUtils;
import com.zzz.robot201.utils.ToastUtil;
import com.zzz.robot201.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:设备绑定列表
 * ================================================
 */
public class EquipmentActivity extends BaseViewActivity<EquipmentPresenter> implements EquipmentContract.View {
    @BindView(R.id.btn_back)
    Button btnBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rcy_equipment)
    RecyclerView rcyEquipment;
    @BindView(R.id.btn_cancel)
    Button btnCancel;
    @BindView(R.id.btn_sure)
    Button btnSure;
    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.btn_scan)
    Button btnScan;
    @BindView(R.id.btn_edit)
    Button btnEdit;
    private EquipmentAdapter equipmentAdapter;
    private int pageNum = 1;
    private int pageSize = 10;
    private String deviceCode;
    private String deviceIP;
    private int devicePort;
    private List<EquipmentBean.DataBean> list = new ArrayList<>();
    private int connectStatus;
    private int connectStatus2;
    private String isConnected = "";

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerEquipmentComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_equipment; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        setTitle(tvTitle, UIUtils.getString(R.string.title_equipment));
        btnScan.setVisibility(View.VISIBLE);
        btnEdit.setVisibility(View.VISIBLE);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this); //线性布局的意思,实现ListView功能
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rcyEquipment.setLayoutManager(layoutManager);
        equipmentAdapter = new EquipmentAdapter();
        rcyEquipment.setAdapter(equipmentAdapter);
        equipmentAdapter.setOnItemClickListener(new EquipmentAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View view) {
                for (int i = 0; i < list.size(); i++) {
                    if (i == position) {
                        list.get(i).setSelect(true);
                    } else {
                        list.get(i).setSelect(false);
                    }
                    equipmentAdapter.notifyDataSetChanged();
                }
                deviceCode = list.get(position).getDeviceCode();
                deviceIP = list.get(position).getDeviceIp();
                devicePort = list.get(position).getDevicePort();
                connectStatus = list.get(position).getConnectStatus();
                connectStatus2 = list.get(position).getConnectStatus2();
                isConnected = list.get(position).getIsConnected();
            }
        });
//        if (mPresenter != null) mPresenter.getEquipment(pageNum + "", pageSize + "");
        list.clear();
        list.add(new EquipmentBean.DataBean("00000001"," 192.168.1.105",8889));
        list.add(new EquipmentBean.DataBean("00000002"," 192.168.1.115",8889));
        list.get(0).setSelect(true);
        deviceCode = list.get(0).getDeviceCode();
        deviceIP = list.get(0).getDeviceIp();
        devicePort = list.get(0).getDevicePort();
        equipmentAdapter.setNewData(list);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
        if ("该设备您已绑定".equals(message)) {

        }
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_back, R.id.btn_cancel, R.id.btn_sure, R.id.iv_right, R.id.iv_left, R.id.btn_scan,R.id.btn_edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                killMyself();
                break;
            case R.id.btn_cancel:
                killMyself();
                break;
            case R.id.btn_edit:
                showDilog();
                break;
            case R.id.btn_sure:
//                if (isConnected.equals("connecting")) {
//                    ToastUtil.showToast("别人连接中");
//                    return;
//                }
//                if (connectStatus == 1) {
//                    SpUtils.put(this, AppContent.SaveInfoKey.DEVICECODE, deviceCode);
//                    SpUtils.put(this, AppContent.SaveInfoKey.DEVICEIP, deviceIP);
//                    SpUtils.put(this, AppContent.SaveInfoKey.DEVICEPORT, devicePort);
//                    launchActivity(new Intent(this, HomeActivity.class));
//                } else {
//                    if (mPresenter != null) mPresenter.getScanBind(deviceCode);
//                }
                SpUtils.put(EquipmentActivity.this, AppContent.SaveInfoKey.DEVICECODE, deviceCode);
                SpUtils.put(EquipmentActivity.this, AppContent.SaveInfoKey.DEVICEIP, deviceIP);
                SpUtils.put(EquipmentActivity.this, AppContent.SaveInfoKey.DEVICEPORT, devicePort);
                launchActivity(new Intent(EquipmentActivity.this, HomeActivity.class));
                break;
            case R.id.iv_right:
                break;
            case R.id.iv_left:
                break;
            case R.id.btn_scan:
                startActivity(new Intent(this, DevicesActivity.class));
                break;
        }
    }

    private void showDilog() {
        final AlertDialog.Builder editDialog = new AlertDialog.Builder(EquipmentActivity.this);
        LayoutInflater inflater = LayoutInflater.from(EquipmentActivity.this);
        View v = inflater.inflate(R.layout.equiment_dialog, null);
        final EditText ip = v.findViewById(R.id.ed_ip);
        final EditText dk = v.findViewById(R.id.ed_dk);
        final EditText id = v.findViewById(R.id.ed_id);

        Button btnSure = v.findViewById(R.id.btn_ep_sure);
        editDialog.setView(v);
//
        final AlertDialog alertDialog = editDialog.create();
        alertDialog.show();
//        alertDialog.setCancelable(true);
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String ipText = ip.getText().toString();
                String dkText = dk.getText().toString();
                String idText = id.getText().toString();
                if (TextUtils.isEmpty(ipText)){
                    ToastUtil.showToast("请输入IP");
                    return;
                }

                if (TextUtils.isEmpty(dkText)){
                    ToastUtil.showToast("请输入端口号");
                    return;
                }

                if (TextUtils.isEmpty(idText)){
                    ToastUtil.showToast("请输入编号ID");
                    return;
                }

                SpUtils.put(EquipmentActivity.this, AppContent.SaveInfoKey.DEVICECODE, idText);
                SpUtils.put(EquipmentActivity.this, AppContent.SaveInfoKey.DEVICEIP, ipText);
                SpUtils.put(EquipmentActivity.this, AppContent.SaveInfoKey.DEVICEPORT, Integer.parseInt(dkText));
                alertDialog.dismiss();
                launchActivity(new Intent(EquipmentActivity.this, HomeActivity.class));

            }
        });

    }

    @Override
    public void getEquipment(EquipmentBean code) {
        if (code.getData().size() >= 3) {
            ivRight.setVisibility(View.VISIBLE);
        }
        list.clear();
        list.addAll(code.getData());
        list.get(0).setSelect(true);
        deviceCode = list.get(0).getDeviceCode();
        deviceIP = list.get(0).getDeviceIp();
        devicePort = list.get(0).getDevicePort();
        connectStatus = list.get(0).getConnectStatus();
        connectStatus2 = list.get(0).getConnectStatus2();
        equipmentAdapter.setNewData(list);
    }

    @Override
    public void getScanBind(BaseBean baseBean) {
        ToastUtil.showToast(baseBean.getMsg());
        SpUtils.put(this, AppContent.SaveInfoKey.DEVICECODE, deviceCode);
        SpUtils.put(this, AppContent.SaveInfoKey.DEVICEIP, deviceIP);
        SpUtils.put(this, AppContent.SaveInfoKey.DEVICEPORT, devicePort);
        launchActivity(new Intent(this, HomeActivity.class));
    }

}
